package com.costaccounting.base.service.impl;

import java.util.ArrayList;
import java.util.List;

import com.alibaba.fastjson.JSONArray;
import com.costaccounting.base.domain.ZsCategory;
import com.costaccounting.base.domain.ZsItem;
import com.costaccounting.base.service.IZsCategoryService;
import com.costaccounting.base.service.IZsItemService;
import com.costaccounting.common.core.domain.AjaxResult;
import com.costaccounting.common.core.domain.entity.SysDictData;
import com.costaccounting.common.core.domain.entity.SysUser;
import com.costaccounting.common.utils.DateUtils;
import com.costaccounting.system.mapper.SysDictDataMapper;
import com.costaccounting.system.service.ISysDictDataService;
import com.costaccounting.system.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.costaccounting.base.mapper.ZsCategoryPersonConfigMapper;
import com.costaccounting.base.domain.ZsCategoryPersonConfig;
import com.costaccounting.base.service.IZsCategoryPersonConfigService;

/**
 * 人员类目分配配置Service业务层处理
 *
 * @author H
 * @date 2021-11-06
 */
@Service
public class ZsCategoryPersonConfigServiceImpl implements IZsCategoryPersonConfigService {
    @Autowired
    private ZsCategoryPersonConfigMapper zsCategoryPersonConfigMapper;
    @Autowired
    private ISysUserService userService;
    @Autowired
    private IZsCategoryService categoryService;
    @Autowired
    private SysDictDataMapper dictDataMapper;
    @Autowired
    private IZsItemService itemService;
    @Autowired
    private ISysDictDataService dictDataService;


    /**
     * 查询人员类目分配配置
     *
     * @param id 人员类目分配配置主键
     * @return 人员类目分配配置
     */
    @Override
    public ZsCategoryPersonConfig selectZsCategoryPersonConfigById(Long id) {
        return zsCategoryPersonConfigMapper.selectZsCategoryPersonConfigById(id);
    }

    @Override
    public AjaxResult getInfo(Long id) {
        AjaxResult ajax = AjaxResult.success();
        ZsCategoryPersonConfig categoryPersonConfig = zsCategoryPersonConfigMapper.selectZsCategoryPersonConfigById(id);

        //楼号
        ZsItem zsItem = itemService.selectZsItemById(categoryPersonConfig.getItemId());
        SysDictData dictData = new SysDictData();
        dictData.setDictType("building_no");
        dictData.setStatus("0");

        List<SysDictData> buildingOptions = dictDataService.selectDictDataList(dictData);
        ajax.put("buildingOptions", buildingOptions);

        //类目
        //获取未选择的类目（加上自己）
        ZsCategoryPersonConfig categoryPersonConfig1=new ZsCategoryPersonConfig();
        categoryPersonConfig1.setId(categoryPersonConfig.getItemId());
        categoryPersonConfig1.setItemId(zsItem.getId());
        categoryPersonConfig1.setStatus("5");
        List<ZsCategory> categoryList= categoryService.getNoList(categoryPersonConfig1);

        String[] buildIds = categoryPersonConfig.getBuildingIds().split(",");
        JSONArray categoryIdList = new JSONArray();
        if (categoryPersonConfig.getCategoryList() != null) {
            categoryIdList = JSONArray.parseArray(categoryPersonConfig.getCategoryList());

        }
        ajax.put("categoryOptions", categoryList);
        ajax.put("buildingIdList",buildIds);
        ajax.put("categoryIdList",categoryIdList);
        ajax.put(AjaxResult.DATA_TAG, categoryPersonConfig);
        return ajax;
    }

    /**
     * 查询人员类目分配配置列表
     *
     * @param zsCategoryPersonConfig 人员类目分配配置
     * @return 人员类目分配配置
     */
    @Override
    public List<ZsCategoryPersonConfig> selectZsCategoryPersonConfigList(ZsCategoryPersonConfig zsCategoryPersonConfig) {
        return zsCategoryPersonConfigMapper.selectZsCategoryPersonConfigList(zsCategoryPersonConfig);
    }

    /**
     * 新增人员类目分配配置
     *
     * @param zsCategoryPersonConfig 人员类目分配配置
     * @return 结果
     */
    @Override
    public int insertZsCategoryPersonConfig(ZsCategoryPersonConfig zsCategoryPersonConfig) {

        String categoryIds = "";
        String buildingIds = "";
        List<Long> cateIds = new ArrayList<>();
        List<List<Long>> categoryIdList = zsCategoryPersonConfig.getCategoryIdList();
        if (null != categoryIdList && 0 < categoryIdList.size()) {
            for (int i = 0; i < categoryIdList.size(); i++) {
                List<Long> idList = categoryIdList.get(i);

                if (0 == i) {
                    categoryIds = idList.get(idList.size() - 1).toString();
                } else {
                    categoryIds = categoryIds + "," + idList.get(idList.size() - 1).toString();
                }
                cateIds.add(idList.get(idList.size() - 1));
            }
        }
        List<Long> buildingIdList = zsCategoryPersonConfig.getBuildingIdList();
        String buildingNos = "";
        if (null != buildingIdList && 0 < buildingIdList.size()) {
            for (int i = 0; i < buildingIdList.size(); i++) {
                Long buildingId = buildingIdList.get(i);
                if (0 == i) {
                    buildingIds = buildingId.toString();
                    buildingNos = dictDataMapper.selectDictLabel("building_no", buildingId.toString());
                } else {
                    buildingIds = buildingIds + "," + buildingId.toString();
                    buildingNos = buildingNos + "," + dictDataMapper.selectDictLabel("building_no", buildingId.toString());
                }

            }
        }
        zsCategoryPersonConfig.setCategoryId(categoryIds);
        zsCategoryPersonConfig.setBuildingIds(buildingIds);

        ZsCategory category = new ZsCategory();
        category.setIds(cateIds);
        List<ZsCategory> categories = categoryService.selectZsCategoryList(category);
        String cateNames = "";
        for (int i = 0; i < categories.size(); i++) {
            if (0 == i) {
                cateNames = categories.get(i).getName().toString();
            } else {
                cateNames = cateNames + "," + categories.get(i).getName().toString();
            }
        }
        String nickName = userService.selectUserByUserName(zsCategoryPersonConfig.getLeaderCode()).getNickName();

        zsCategoryPersonConfig.setRemark(nickName+"msmsm"+cateNames+"msmsm"+buildingNos);
        zsCategoryPersonConfig.setCategoryList(zsCategoryPersonConfig.getCategoryIdList().toString());
        zsCategoryPersonConfig.setCreateTime(DateUtils.getNowDate());
        return zsCategoryPersonConfigMapper.insertZsCategoryPersonConfig(zsCategoryPersonConfig);
    }

    /**
     * 修改人员类目分配配置
     *
     * @param zsCategoryPersonConfig 人员类目分配配置
     * @return 结果
     */
    @Override
    public int updateZsCategoryPersonConfig(ZsCategoryPersonConfig zsCategoryPersonConfig) {
        String categoryIds = "";
        String buildingIds = "";
        List<Long> cateIds = new ArrayList<>();
        List<List<Long>> categoryIdList = zsCategoryPersonConfig.getCategoryIdList();
        if (null != categoryIdList && 0 < categoryIdList.size()) {
            for (int i = 0; i < categoryIdList.size(); i++) {
                List<Long> idList = categoryIdList.get(i);

                if (0 == i) {
                    categoryIds = idList.get(idList.size() - 1).toString();
                } else {
                    categoryIds = categoryIds + "," + idList.get(idList.size() - 1).toString();
                }
                cateIds.add(idList.get(idList.size() - 1));
            }
        }
        List<Long> buildingIdList = zsCategoryPersonConfig.getBuildingIdList();
        String buildingNos = "";
        if (null != buildingIdList && 0 < buildingIdList.size()) {
            for (int i = 0; i < buildingIdList.size(); i++) {
                Long buildingId = buildingIdList.get(i);
                if (0 == i) {
                    buildingIds = buildingId.toString();
                    buildingNos = dictDataMapper.selectDictLabel("building_no", buildingId.toString());
                } else {
                    buildingIds = buildingIds + "," + buildingId.toString();
                    buildingNos = buildingNos + "," + dictDataMapper.selectDictLabel("building_no", buildingId.toString());
                }

            }
        }
        zsCategoryPersonConfig.setCategoryId(categoryIds);
        zsCategoryPersonConfig.setBuildingIds(buildingIds);

        ZsCategory category = new ZsCategory();
        category.setIds(cateIds);
        List<ZsCategory> categories = categoryService.selectZsCategoryList(category);
        String cateNames = "";
        for (int i = 0; i < categories.size(); i++) {
            if (0 == i) {
                cateNames = categories.get(i).getName().toString();
            } else {
                cateNames = cateNames + "," + categories.get(i).getName().toString();
            }
        }
        String nickName = userService.selectUserByUserName(zsCategoryPersonConfig.getLeaderCode()).getNickName();

        zsCategoryPersonConfig.setRemark(nickName+"msmsm"+cateNames+"msmsm"+buildingNos);
        zsCategoryPersonConfig.setCategoryList(zsCategoryPersonConfig.getCategoryIdList().toString());
        zsCategoryPersonConfig.setUpdateTime(DateUtils.getNowDate());
        return zsCategoryPersonConfigMapper.updateZsCategoryPersonConfig(zsCategoryPersonConfig);
    }

    /**
     * 批量删除人员类目分配配置
     *
     * @param ids 需要删除的人员类目分配配置主键
     * @return 结果
     */
    @Override
    public int deleteZsCategoryPersonConfigByIds(Long[] ids) {
        return zsCategoryPersonConfigMapper.deleteZsCategoryPersonConfigByIds(ids);
    }

    /**
     * 删除人员类目分配配置信息
     *
     * @param id 人员类目分配配置主键
     * @return 结果
     */
    @Override
    public int deleteZsCategoryPersonConfigById(Long id) {
        return zsCategoryPersonConfigMapper.deleteZsCategoryPersonConfigById(id);
    }

    @Override
    public List<ZsCategoryPersonConfig> selectList(ZsCategoryPersonConfig zsCategoryPersonConfig) {
//        List<ZsCategoryPersonConfig> zsCategoryPersonConfigs = zsCategoryPersonConfigMapper.selectZsCategoryPersonConfigList(zsCategoryPersonConfig);
//        for (int i = 0; i < zsCategoryPersonConfigs.size(); i++) {
//            ZsCategoryPersonConfig categoryPersonConfig = zsCategoryPersonConfigs.get(i);
//            categoryPersonConfig.setLeaderName(userService.selectUserByUserName(categoryPersonConfig.getLeaderCode()).getNickName());
//
//
//        }

        return null;
    }
}
